/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package retrofit2;

import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

import static retrofit2.Utils.methodError;

abstract class ServiceMethod<T> {
    /**
     * 解析注解
     */
    static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
        //解析接口上的请求方法的注解，以及方法形参上的注解，并把信息转换为RequestFactory实例
        RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);
        //获取返回值上的泛型
        Type returnType = method.getGenericReturnType();
        //泛型不可以有通配符
        if (Utils.hasUnresolvableType(returnType)) {
            throw methodError(
                    method,
                    "Method return type must not include a type variable or wildcard: %s",
                    returnType);
        }
        //返回值的泛型不能是Void类型
        if (returnType == void.class) {
            throw methodError(method, "Service methods cannot return void.");
        }
        //进行返回值适配和转换器
        return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
    }

    /**
     * 执行方法
     *
     * @param args 方法参数
     */
    abstract @Nullable
    T invoke(Object[] args);
}